# 3.炸弹概率分析
# 斗地主游戏中，炸弹可以是四张一样数字或字母的牌，如：四张K、四张5，也可以是一对大小王。请计算斗地主游戏中，炸弹出现的概率。
import random
def boom():
    farmer_flag = 0  # 记录农民获得炸弹的次数
    landlord_flag = 0  # 记录地主获得炸弹的次数
    # 模拟游戏进行10000次
    for i in range(10000):
        farmer = []  # 农民手牌
        landlord = []  # 地主手牌
        f_card = 0  # 记录农民手牌数量
        while f_card < 17:
            cards = random.choice("123456789XJQKBM")  # 随机选择一张牌
            if farmer.count(cards) >= 4:  # 判断是否已有四张相同的牌
                pass
            elif cards == "B" and farmer.count("B") == 1:  # 判断是否已有一张大小王
                pass
            elif cards == "M" and farmer.count("M") == 1:  # 判断是否已有一张大小王
                pass
            else:
                farmer.append(cards)  # 添加牌到农民手牌
                f_card += 1
        l_card = 0  # 记录地主手牌数量
        # 地主随机抽取20张牌
        while l_card < 20:
            cards = random.choice("123456789XJQKBM")  # 随机选择一张牌
            if landlord.count(cards) >= 4:  # 判断是否已有四张相同的牌
                pass
            elif cards == "B" and landlord.count("B") == 1:  # 判断是否已有一张大小王
                pass
            elif cards == "M" and landlord.count("M") == 1:  # 判断是否已有一张大小王
                pass
            else:
                landlord.append(cards)  # 添加牌到地主手牌
                l_card += 1
        # 判断农民是否获得炸弹
        for i in "123456789XJQK":
            if farmer.count(i) == 4 or ("B" in farmer and "M" in farmer):  # 判断是否有四张相同的牌或大小王
                farmer_flag += 1
                break
        # 判断地主是否获得炸弹
        for i in "123456789XJQK":
            if landlord.count(i) == 4 or ("B" in landlord and "M" in landlord):  # 判断是否有四张相同的牌或大小王
                landlord_flag += 1
                break
    # 返回农民和地主获得炸弹的概率
    return farmer_flag / 10000, landlord_flag / 10000
# 输出农民和地主获得炸弹的概率
print("农民获得炸弹的概率为{:.2f}%，地主获得炸弹的概率为{:.2f}%".format(boom()[0] * 100, boom()[1] * 100))

